/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package battleroom;

import com.jme3.app.SimpleApplication;
import com.jme3.bounding.BoundingBox;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;
import java.io.IOException;

/**
 *
 * @author thornz & adam
 */
public class TargetControl extends AbstractControl {
    private Vector3f size;
    private Node root;
    private Main app;
    public TargetControl(Main app, Node root, float sX ,float sY, float sZ)
    {
        this.app = app;
        size = new Vector3f(sX, sY, sZ);
        this.root = root;
    }
    
    @Override
    protected void controlUpdate(float tpf) {
    }
    
    public void onHit()
    {
        root.detachChild(getSpatial());
        GameState.get().setTargetsHit(GameState.get().getTargetsHit()+1);
    }
    
    public Control cloneForSpatial(Spatial spatial) {
        TargetControl clone = new TargetControl(app, root, size.getX(), size.getY(), size.getZ());
        clone.size = this.size;
        return clone;
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }  
    
    @Override
    public void setSpatial(Spatial spat)
    {
        super.setSpatial(spat);
        Geometry geom = (Geometry)spat;
        BoundingBox bound = (BoundingBox)geom.getWorldBound();
        BoxCollisionShape box = new BoxCollisionShape(bound.getExtent(new Vector3f()));
        RigidBodyControl rb = new RigidBodyControl(box);
        spatial.addControl(rb);
        app.bulletAppState.getPhysicsSpace().add(rb);
    }
    
}
